//
//  SFPBodyRecordModel.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/10.
//

import Foundation
class SFPBodyRecordViewModel{
    var dataSource = [SFPBodyRecordModel]()
}
extension SFPBodyRecordViewModel{
    func loadData(onSuccess: ((_ models: [SFPBodyRecordModel]) ->Void)?){
        let dataHelper = SFPBodyRecordModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: SFPBodyRecordModel.self) as! [SFPBodyRecordModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy.MM.dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
    
}

@objcMembers
class SFPBodyRecordModel: SFPModel,Codable {
    var weight = ""
    var tyre = ""
    var date = ""
    var days = ""
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: SFPBodyRecordModel.self)
    }
}
